<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>material</title>
    <script src="__LAYUI__/layui.js"></script>
    <link href="__LAYUI__/css/layui.css" rel="stylesheet"/>
    <style>
        .btn {
            display: none;
        }
    </style>
</head>
<body>
<div style="height: 40px;text-align: center;line-height: 40px" id="btn">
</div>
<script type="text/html" id="insert">
    <div style="float: right">
        <button class="layui-btn layui-btn-normal" id="staffInsert">新增</button>
    </div>
</script>

<table id="table" lay-filter="text"></table>


<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-mini" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-warm layui-btn-mini" lay-event="txt">txt文件</a>
    <a class="layui-btn layui-btn-danger layui-btn-mini" lay-event="del">删除</a>
</script>

<script>
    layui.use(['table', 'jquery', 'layer'], function () {
        var table = layui.table;
        var $ = layui.$ //重点处
            , layer = layui.layer;
        if ({$user_level} == '0') {
            $('#btn').html($('#insert').html());
            table.render({
                id: 'materTable',
                elem: 'table'
                ,
                page: true
                ,
                limit: 10
                ,
                skin: 'line'
                ,
                even: true
                ,
                size: 'lg'
                ,

                cols: [[
                    {field: 'book_id', title: 'id', width: 130}
                    , {field: 'book_name', title: '书名', event: 'setSign', width: 230, style: 'cursor: pointer;'}
                    , {field: 'book_company', title: '出版社', width: 230}
                    , {field: 'book_word_count', title: '字数', width: 150}
                    , {field: 'status', title: '书号状态', width: 200}
                    , {fixed: 'right', width: 200, align: 'center', toolbar: '#barDemo'}
                ]]
                ,
                url: '/index/Material/getBooks'
                , response: {
                    statusName: 'code' //数据状态的字段名称，默认：code
                    , statusCode: 1 //成功的状态码，默认：0
                }
            });
        } else {

            table.render({
                id: 'materTable',
                elem: 'table'
                ,
                page: true
                ,
                limit: 10
                ,
                skin: 'line'
                ,
                even: true
                ,
                size: 'lg'
                ,

                cols: [[
                    {field: 'book_id', title: 'id', width: 200}
                    , {field: 'book_name', title: '书名', event: 'setSign', width: 370, style: 'cursor: pointer;'}
                    , {field: 'book_company', title: '出版社', width: 370}
                    , {field: 'book_word_count', title: '字数', width: 200}
                    , {field: 'status', title: '书号状态', width: 200},
                ]]
                ,
                url: '/index/Material/getBooks'
                , response: {
                    statusName: 'code' //数据状态的字段名称，默认：code
                    , statusCode: 1 //成功的状态码，默认：0
                }
            });
        }

        table.on('tool(text)', function (obj) {
            var data = obj.data;
            if (obj.event === 'setSign') {
                window.location.href = '/index/Material/getBookInfo?book_id=' + data.book_id;
            }
            if (obj.event === 'edit') {
                layer.open({
                    type: 2,
                    offset: '0px',
                    title: '教材修改',
                    scrollbar: false,
                    resize: false,
                    area: ['600px', '570px'],
                    shadeClose: true,
                    content: ['/index/Material/changeMaterial?book_id=' + data.book_id, 'no'],
                });
            }
            if (obj.event === 'txt') {
//                layer.open({
//                    type: 2,
//                    time:5000,
//                    title:'TXT文件下载',
//                    content: ['/index/File/backUp?book_id='+data.book_id,'no'] //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
//                });
                window.location.href='/index/File/backUpMate?book_id='+data.book_id;
            }
            if (obj.event === 'del') {
                layer.open({
                    type: 0,
                    content: '确定删除?',
                    yes: function (index, layero) {
                        $.ajax({
                            type: 'post',
                            url: '/index/Material/delBook  ',
                            data: {
                                'book_id': data.book_id
                            },
                            success: function (result) {
                                if (result.code) {
                                    layer.msg(result.msg, {
                                        icon: 1,
                                        time: 3000 //2秒关闭（如果不配置，默认是3秒）
                                    });
                                }
                                table.reload('materTable', {});
                            }
                        });

                        layer.close(index);
                    }

                });
            }
        });

        $('#staffInsert').click(function () {
            var layer = layui.layer;
            layer.open({
                type: 2,
                offset: '0px',
                title: '新增教材',
                scrollbar: false,
                resize: false,
                area: ['600px', '570px'],
                shadeClose: true,
                content: ['/index/Material/insertMaterial', 'no'],
            });
        });
    });

</script>


</body>
</html>